#include "stm32f10x.h"
#include "SD.h"
#include "uart.h"
#include "GPRS.h"
#include "GPS.h"
#include "uCOS_Include.h"
#include <stdio.h>

/*超速值的定义*/
//int16_t shili_to_shuirao=60;		  //十里画廊——水绕四门 60Km/h	 55
//int16_t laomo_to_huangshi=60;		  //老磨湾——黄石寨索道下 60Km/h  55
//int16_t sancha_to_tongqiang=42;	      //三岔口路——铜墙铁壁 42Km/h	  40
//int16_t Other_Speeding_Value=48;     //其它 48Km/h	   45

int16_t shili_to_shuirao=50;		  //十里画廊——水绕四门   50蜂鸣提示，60Km/h超速上报	 55
int16_t laomo_to_huangshi=50;		  //老磨湾——黄石寨索道下 50蜂鸣提示，60Km/h超速上报  55
int16_t sancha_to_tongqiang=35;	      //三岔口路——铜墙铁壁   35蜂鸣提示，42Km/h超速上报	  40
int16_t Other_Speeding_Value=40;      //其它 40蜂鸣提示，48Km/h超速上报	   45

int16_t Corner_Speeding_Value=35;	  //弯道 35蜂鸣提示	42超速报警   32
int16_t Straight_Speeding_Value=99;   //初始化在景区外超速报警   99

uint16_t heart_rate_count = 0;			// 用于发送心跳包的计数，每5次发一个心跳包，即25S

/*不同路段超速值变更的标志位*/
u8 Position_Flag=1;
u8 Position_Init=1;
/*不同路段超速值变更的标志位*/
u8 Flag_Corner=0;	 //检测弯道的标志位
u8 Flag_Into=0;   //进弯标志位，1表示出弯
u8 Site;			// 第几个站点（2-33），0表示离站，1表示弯道
u8 Corner_Speech;		 //第一次进弯道标志位

u8 Alarm_W_Flag;
u8  First_Dot=0;

/*定时器形成自锁标志位*/
u8 Lock_Flag_Z=0;		//用于直道测速形成自锁
u8 Lock_TIM3_Z=0;		//定时器3用于直道测速形成自锁
u8 Time_Over_Z=0;	//直道测速定时器超时

u8 Lock_Flag_W=0;		//用于弯道测速形成自锁
u8 Lock_TIM3_W=0;		//定时器3用于弯道测速形成自锁
u8 Time_Over_W=0;   //弯道测速定时器超时

u8 Buzzer;			//蜂鸣器响标志位
u8 Lock_TIM2;		//用于定时器2形成自锁
u8 Stop_Time_Over=0;	//车辆停的时间超时
u8 Stop=1;			//离站播放语音标志位

/*GPS数据转换成GPRS发送数据的中间变量*/
u8 time_chang[]={"12.34"};
int16_t time_int;

/*经纬度、航向角、速度、状态的转换变量*/
int16_t	Course ;
int32_t Weidu,Jingdu;
float Speed;  
u8 Statue;		 //定位状态
u8 Alarm_Location_W=0;	//报警区域位置显示
u8 Alarm_Location_Z=0;	//报警区域位置显示
u8 Alarm_Location_Flag=0;

extern OS_FLAG_GRP *Flag_Grp;
INT8U err;
extern uint16_t Update_Rate;
extern u8       GPRS_Send_flag;
extern u8       Buf_flag;
extern int16_t  Speed_Compare;
extern u8       Compare_Fail;	
extern u8       Speed_Detection;		  
 							//Dot_Course_Angle[i][0]为第一个进弯点的航向角 32
							 
int16_t Dot_Course_Angle[64][2]={ {270,280},{78,73}, {304,145},{205,73}, {271,213},{240,164},{210,157},{354,176},
								  {183,84} ,{251,154},{193,67}, {318,32}, {28,21},  {85,5},   {183,196}, //B15
								  {149,60} ,{245,161},{222,142},{243,185},{192,142},{191,115},{116,359},{332,47},
								  {300,42},  {16,192},{340,330}, {336,186},{276,339},{259,153},{170,271},{102,281},	//B31
								  {240,305},{335,352},{75,335},{194,295}};	//B32
						   //Stand_Course_Angle[i][0]为进站点的航向角  35
int16_t Stand_Course_Angle[64][2]={{222,313},{339,194},{255,82}, {18,200}, {207,15}, {137,359},{299,178},{349,152},{178,308},//9
								   {76,187}, {219,86}, {118,227},{329,154},{202,13}, {218,40}, {165,296},{345,85}, {120,265},
								   {110,340},{158,23}, {330,150},{287,64}, {120,265},{339,190},{302,107},{287,112},{149,323},
								   {342,162},{48,308}, {209,103},{202,17}, {249,71}, {33,249}, {84,246}, {315,130}, {295,95},//36
																     //40~42蔡家峪				   //43~44铜墙铁壁
								   {58,90},{108,240}, {127,300},   {0,0},{0,0},{0,0},           {0,0},{0,0},
									//45~46	黄石寨索道下、琵琶界（龙凤庵下站）
								   {165,43},{29,151},
								   {0,0},{0,0},{0,0},{0,0},{0,0}};
					   //进弯点	纬度（8）、经度（9）//出弯点		29215569,110302111	
					   			
int32_t dot[64][4]={{29213755,110316481,29213826,110316576},{29213777,110317369,29213929,110317381},  //B1
					{29213988,110315066,29214428,110314670},{29215581,110312098,29215402,110311690},  //B3
					{29215791,110310181,29216032,110309910},{29216359,110308767,29216042,110309175},  //B5
					{29215068,110307374,29215000,110306845},{29215703,110304487,29216234,110304204},  //B7

					{29219992,110301569,29219590,110301264},{29218712,110299369,29218973,110298921},  //B9
					{29218139,110296067,29217754,110295569},{29198248,110284105,29198140,110283648},  //B11
					{29194470,110281495,29194354,110281609},{29194660,110282388,29194004,110282129},  //B13
					{29191592,110281535,29191800,110281704},										  //B14

					{29234287,110297199,29233841,110297160},{29234861,110284590,29234867,110283970},  //B16
					{29236464,110284095,29236479,110283533},{29234729,110275839,29234714,110275548},  //B18
					{29234903,110274525,29234847,110274060},  //B19
					{29234691,110273532,29234520,110273273},{29233987,110271572,29233531,110271912},   //B21
					{29227845,110271174,29227800,110270807},{29229850,110268340,29230209,110267944},   //B23
					{29230435,110267952,29230562,110268656},{29239387,110263823,29239291,110263657},   //B25
					{29228296,110268501,29228697,110268203},{29229695,110265553,29229546,110265203},   //B27
					{29216065,110257498,29216248,110256839},{29213346,110257490,29212842,110257806},   //B29
					{29209772,110269380,29209366,110269864},   //23 B30
					{29209015,110244234,29208570,110244134},{29207397,110238582,29207327,110238477},   //B32
					{29206856,110241820,29206637,110242354},{29205510,110243376,29205188,110243562}};  //B34 
					  
										 //进站报站、出站温馨提示
						//1号站点标志门						//2号站点天子上索道下	 
int32_t Stand[64][4]={{29213317,110318056,29212704,110317672},{29225751,110303775,29226065,110303721},	   //,{}
						//3~4号站点十里画廊															 
					{29217494,110294777,29217293,110293944},{29215987,110292824,29216809,110293152},
						  //5~7号站点水绕四门																
					{29207266,110280654,29206790,110280412},{29207974,110280236,29207133,110280207},{29205300,110280460,29206135,110280338},
					  //8号站点 百龙天梯下							   //9号站点  梓木岗
					{29210384,110278277,29210934,110278070},{29192509,110283940,29192005,110284008},
					   //10号站点 天子山索道上
					{29235671,110296270,29235882,110296218},
					//11~12号站点	贺龙公园
					{29231071,110295101,29230818,110294800},{29231750,110293660,29231347,110294030},  //11
					//13~14号站点 天子山
					{29231929,110293335,29232043,110293265},{29232942,110293263,29232608,110293098}, //13
					//15~16号站点	   点将台
					{29232836,110289886,29232614,110289764},{29233149,110288862,29232665,110289298}, //15
					   //17~18号站点  神堂湾
					{29233417,110288795,29233583,110288292},{29233976,110287530,29233757,110287899},
					//19~20号站点	   大观台		
					{29229028,110271407,29228375,110271779},{29227737,110271221,29227869,110271561},
					//21~23号站点	  	 三岔路口
					{29227806,110270455,29227981,110270211},{29228276,110270626,29228252,110270253},{29228244,110269403,29228134,110269771},
						 //24号站点	泗南峪
					{29242013,110263248,29242676,110263122},
					//25~26号站点		 乌龙寨
					{29216789,110254855,29217163,110254166},{29216476,110254779,29216709,110254180},
					//27~28号站点		  天下第一桥
					{29210363,110261053,29209966,110261389},{29208639,110261723,29209142,110261537},
					//29~30号站点		 迷魂台									
					{29208626,110262105,29208640,110262623},{29208486,110263634,29208362,110263321},   //29
					//31~32号站点		后花园
					{29209308,110266573,29208812,110266349},{29209076,110267320,29208770,110266754}, //31
					//33号站点	   	百龙天梯上
					{29209162,110273287,29209561,110273812},							 
					//34号站点		老磨湾						   //35号站点	 黄石寨索道下
					{29191596,110256591,29191818,110257287},{29195416,110251989,29195810,110251515},
					//36号站点		杨家界						  
					{29213678,110235566,29213762,110234872},
					//37~38号站点		百里坡					   
				//	{29209686,110244152,29209405,110244736},{29209076,110244373,29209672,110244413},
					{29209139,110244439,29209686,110244287},{29209697,110242213,29209491,110243477},
					//39号站点	 龙凤庵
					{29201214,110247709,29200893,110248132},
					//40~42号站点	  	 蔡家峪
					{29222492,110303573,0,0},{29223185,110303311,0,0},{29222468,110302667,0,0},
					//43~44号站点        铜墙铁壁  修改速度值
					{29231208,110267625,0,0},{29231352,110267444,0,0},
					 //45~46号站点  黄石寨索道下、琵琶界（龙凤庵下站）
					{29196540,110250799,29195956,110250983},{29198196,110249744,29198594,110249486}	,

					//47~49号站点  标志门、梓木岗、老磨湾 景气外检测
					{29212329,110318501,0,0},{29191755,110284550,0,0},{29192069,110257990,0,0},
					//50~51号站点  杨家界、泗南峪
					{29214096,110234446,0,0},{29243371,110263242,0,0}
					
					};


struct 	 				//GPS数据缓存区
	{
	u8 Buf[6];	   		//GPS语句标识符缓存空间
	u8 Buf_Time[7];   //GPS时间缓存空间
	u8 Buf_WeiDu[10];   //GPS纬度缓存空间
	u8 Buf_JingDu[11];	//GPS经度缓存空间
	u8 Buf_Statu;	   //GPS卫星状态缓存空间
	u8 Buf_Speed[6];		//GPS速度缓存空间
	u8 Buf_Course[6];	   //GPS航向角缓存空间
	u8 Buf_Data[7];	   //GPS日期缓存空间
	
	}GPS;

/************************************
函数功能：GPS数据解析
函数入口：无
函数出口：无
函数说明：把接收到的时间、纬度、经度、定位状态、速度、日期
		  通过串口发送出去
***********************************/
void GPS_Parse()			 //GPS数据解析
{
//	u8 i;

  /* for(i=0;i<4;i++)
	{
	 UART2_SendByte(0x20);		  //空格
	}
	UART2_SendByte(Point+0x30);	   //	 弯道数
	UART2_SendByte(0x0D);			  //回车
	UART2_SendByte(0x0a);	*/

//   for(i=0;i<4;i++)
//	{
//	 UART2_SendByte(0x20);		  //空格
//	}
//	UART2_SendByte(GPS.Buf_Statu);	   	//定位状态 ,A有效 ，V无效
//	UART2_SendByte(0x0D);			  	//回车
//	UART2_SendByte(0x0a);  		  		//换行

	UART2_SendByte(0x54);			 	//T
	UART2_SendByte(0x3a);			 	//:
	UART2_SendString(GPS.Buf_Time);		//定位时间
	UART2_SendByte(0x0D);
	UART2_SendByte(0x0a);		   
//
//	UART2_SendByte(0x57);			   //W
//	UART2_SendByte(0x3a);
//
//	UART2_SendString(GPS.Buf_WeiDu);	   	//纬度
//	UART2_SendByte(0x0D);
//	UART2_SendByte(0x0a);		
//
//	UART2_SendByte(0x4a);			   //J
//	UART2_SendByte(0x3a);
//
//	UART2_SendString(GPS.Buf_JingDu);   //经度
//	UART2_SendByte(0x0D);
//	UART2_SendByte(0x0a);			 
//
//	UART2_SendByte(0x44);			   //D
//	UART2_SendByte(0x3a);
//
//	UART2_SendString(GPS.Buf_Data);	//日期
//	UART2_SendByte(0x0D);
//	UART2_SendByte(0x0a);			
//
//	UART2_SendByte(0x53);			  //S
//	UART2_SendByte(0x3a);
//	UART2_SendString(GPS.Buf_Speed);	 //速度
//	UART2_SendByte(0x0D);
//	UART2_SendByte(0x0a);	
//	
//	UART2_SendByte(0x43);			  //C
//	UART2_SendByte(0x3a);
//	UART2_SendString(GPS.Buf_Course);	 //航向角
//	UART2_SendByte(0x0D);
//	UART2_SendByte(0x0a);		

}
/************************************
函数功能：GPS数据缓存发送
函数入口：无
函数出口：无
函数说明：填充发送的GPS数据
***********************************/
void GPS_Data_Buffer_Send(void)
{
	int16_t Temp=0;

	Temp=Speed_Compare;
	if(Alarm_W_Flag==1)
	{	
		Alarm_W_Flag=0;
		Alarm_W=1;
		Function=2;
		Buf_flag=1;
		GPRS_Send_flag=1;
	}	 
	else
	{
		if(Other_Speeding_Value==0)	   //不进行超速检测
		{
			// Modify by yc @ 20160707
			if((heart_rate_count++) % 5 == 0)
				Function = 1;				// 25S一个心跳包维持链路
			else
				Function=3;
			
			GPS_TO_GPRS_Data();	
			Buf_flag=1;
			GPRS_Send_flag=1;
			return ;
		}
		else if((Alarm_Location_Z==0)&&(Alarm_Location_W==0))  //景区外 99Km/h超速报警
		{
			LOG("Outside-speed,Det,Loc,GPS:%d,%d,%d,%X",Temp,Speed_Detection,Alarm_Location_Z,Statue);
			 if((Temp>(Straight_Speeding_Value-2))&&(Temp<120)&&Speed_Detection)
			 {
//				 Function=2;				   //景区外不进行数据上传，全部屏蔽
//				 Alarm=1;
//				 Alarm_Location_Flag=100;
//				 GPS_TO_GPRS_Alarm_Data();			
//				 Buf_flag=1;
//				 GPRS_Send_flag=1;
			 }
			 else
			 {
				 // Modify by yc @ 20160707
				if((heart_rate_count++) % 5 == 0)
					Function = 1;				// 25S一个心跳包维持链路
				else
					Function=3;
				// Modify by yc @ 20160707
				
				  Alarm=0;
				  GPS_TO_GPRS_Data();				
				  Buf_flag=1;
				  GPRS_Send_flag=1;
			 }
			 return ;
		}
				
	//	if((Temp>(Straight_Speeding_Value-2))&&(Temp<75))		  
		if((Temp>(Straight_Speeding_Value*0.2 + Straight_Speeding_Value))&&(Temp<75)) //景区内直道超速报警	 by yq	20150913
		{		
			LOG("Inside-speed,Det,Loc,GPS:%d,%d,%d,%X",Temp,Speed_Detection,Alarm_Location_Z,Statue);
			if(!Speed_Detection||((Alarm_Location_Z==4||Alarm_Location_Z==5||Alarm_Location_Z==34||Alarm_Location_Z==35)&&(Temp<=(shili_to_shuirao*0.2 + shili_to_shuirao))))
			{	
				// Modify by yc @ 20160707
				if((heart_rate_count++) % 5 == 0)
					Function = 1;				// 25S一个心跳包维持链路
				else
					Function=3;
				
				Alarm=0;
				GPS_TO_GPRS_Data();				
				Buf_flag=1;
				GPRS_Send_flag=1;				
			}
			else
			{
				Function=2;
				Alarm=1;
				Alarm_Location_Flag=Alarm_Location_Z;
				GPS_TO_GPRS_Alarm_Data();			
				Buf_flag=1;
				GPRS_Send_flag=1;	
			}																							
		}
		else if(Temp<=(Straight_Speeding_Value*0.2 + Straight_Speeding_Value))
		{
			// Modify by yc @ 20160707
			if((heart_rate_count++) % 5 == 0)
				Function = 1;				// 25S一个心跳包维持链路
			else
				Function=3;
			
			GPS_TO_GPRS_Data();	
		//	UART2_SendString("GPS_TO_GPRS_Data\n");
			Buf_flag=1;
			GPRS_Send_flag=1;
		}
		else
		{
			 GPS_Number=Update_Rate;
	//		 UART2_SendString("GPS_TO_GPRS_Fail\n");
		}
	}	
}

void Get_Speed_Value(void)
{
	float Get_Speed=0;

	Get_Speed=Ascii_to_float(GPS.Buf_Speed);
	Get_Speed=Get_Speed*1.852;	 //速度转换
	Speed_Compare=(int16_t)Get_Speed;
}
/************************************
函数功能：直道超速检测
函数入口：Value超速值，c=1时表示直道，c=0时表示弯道
函数出口：无
函数说明：通过蜂鸣器超速报警
***********************************/
void Speed_Detection_Z(void)
{
	int16_t Temps=0;
	float Speed=0;

	Speed=Ascii_to_float(GPS.Buf_Speed);
	Speed=Speed*1.852;	 //速度转换
	Temps=(int16_t)Speed;
		 												  //因车辆速度与GPS速度有一定误差，具体直道超速蜂鸣在这里调节
	if((Temps>(Straight_Speeding_Value-2))&&(Temps<120))  // by yq 20150913 直道蜂鸣40，十~水和黄-老 50，三-铜 35		   			
	{
/*		if(Time_Over_Z==1)
		{
			TIM3->CR1&=0x03FE;	//失能定时器3
		//	GPIO_ResetBits(GPIOB,GPIO_Pin_9);//如果，超速则蜂鸣器报警；
			Time_Over_Z=0; //形成自锁,在一定范围内，只报警一次						
	//		Send_Command(0x04);	   //请减速
//			Function=2;
//			Alarm=1;
//			GPS_Number=0;
//			Compare_Fail=0;
//			Alarm_Location_Flag=Alarm_Location;
//			GPS_TO_GPRS_Alarm_Data();

			UART2_SendString("Speed_Detection_Z\n");

		//	Buf_flag=1;								
		//	GPRS_Send_flag=1;												
		}
		else
		{
			if(Lock_TIM3_Z)
			{
				Lock_TIM3_Z=0;
				Time_Over_Z=4;
				TIM3->CR1|=0x0001;    //使能定时器3
		//		Send_Command(0x04);	   //请减速
				OSFlagPost(Flag_Grp,0x02,OS_FLAG_SET,&err);
			}
			
		}	   */
		OSFlagPost(Flag_Grp,0x02,OS_FLAG_SET,&err);
		
	}
	else
	{
		if((Time_Over_W<3)||(Site!=1))
		{
			GPIO_SetBits(GPIOB,GPIO_Pin_9);
		}
//		TIM3->CR1&=0x03FE;	//失能定时器3
//		Time_Over_Z=0;
//		Alarm=0;
//		Lock_Flag_Z=1;
//		Lock_TIM3_Z=1;
		OSFlagPost(Flag_Grp,0x02,OS_FLAG_CLR,&err);
	}
	
}
//弯道
void Speed_Detection_W(void)
{
	int16_t Temps=0;
	float Speed=0;

	Speed=Ascii_to_float(GPS.Buf_Speed);
	Speed=Speed*1.852;	 //速度转换
	Temps=(int16_t)Speed;
	  		
	if((Temps>(Corner_Speeding_Value-2))&&(Temps<60))	  //因车辆速度与GPS速度有一定误差，具体弯道超速蜂鸣在这里调节
	  {
		   	if(Lock_Flag_W)
			{		
				GPIO_ResetBits(GPIOB,GPIO_Pin_9);//如果，超速则蜂鸣器报警；
				
				if(Temps>(Corner_Speeding_Value*0.2 + Corner_Speeding_Value))	 //弯道要超过提示速度的20%才上报报警信息，即35*20%+35=42  by yq 20150913
				{												
					Lock_Flag_W=0;				
			//		Send_Command(0x04);	   //请减速				
			//		Function=2;
					Alarm_W_Flag=1;
			//		Alarm_W=1;
			//		GPS_Number=0;
			//		Compare_Fail=0;
					Alarm_Location_Flag=Alarm_Location_W;
					GPS_TO_GPRS_Alarm_Data();	
			//		GPRS_Send_flag=1;
			//		Buf_flag=1;	
				}					
			}
			if(Time_Over_W==4)
			{
				if(Lock_TIM3_W)
				{
					Lock_Flag_W=1;
					Lock_TIM3_W=0;
				}		
				
			}
			if(Time_Over_W==2)
			{			
				TIM2->CR1&=0x03FE;	//失能定时器2
				Time_Over_W=0;
				Lock_Flag_W=0;		//形成自锁,在一定范围内，只报警一次
				GPIO_SetBits(GPIOB,GPIO_Pin_9);				
			}
			
		}
		else
		{
		//	Function=3;
		//	Alarm_W=0;
			TIM2->CR1&=0x03FE;	//失能定时器2
			Time_Over_W=0;
			Lock_Flag_W=0;						
			GPIO_SetBits(GPIOB,GPIO_Pin_9);
		}

}

void Speed_QR(u8 c)
{
	int16_t Temps=0;
	Speed=0;

	Speed=Ascii_to_float(GPS.Buf_Speed);
	Speed=Speed*1.852;	 //速度转换
	Temps=(int16_t)Speed;
	if(c)		   //直道
	{	 
		if((Temps>Straight_Speeding_Value)&&(Temps>42))
		{										
			Alarm=1;				
		}
		else
		{			
			Alarm=0;				
		}

	}
	else					  //弯道
	{
		if((Temps>Corner_Speeding_Value)&&(Temps>35))
		{			   	
			Alarm_W=1;				
		}
		else
		{		
			Alarm_W=0;							
		}

	}	
}

/************************************
函数功能：车辆停站检测
函数入口：无
函数出口：无
函数说明：如果，车辆的速度低于了一定值，则认为车辆已经停下来了，
			如果车辆停的时间过长，则需要设置离站语音播放标志位。
***********************************/
void Left_Stand_Speech(void)
{
	Speed=Ascii_to_float(GPS.Buf_Speed);
	Speed=Speed*1.852;	 //速度转换
	if(Speed<3)
	{
	   if(Lock_TIM2)
		{
			Lock_TIM2=0;
			Stop_Time_Over=0;		//2s定时
			Site=0;				
		}	   	   	   
	    else if(Stop_Time_Over>8)
		{								
			Stop=1;	
			Stop_Time_Over=0;		
		}				
	}
	else
	{	 	
		Lock_TIM2=1;
	}
}
/************************************
函数功能：检测进站点、离站点、进弯点
函数入口：无
函数出口：无
函数说明：通过LED指示进弯点，进弯点不指示
***********************************/
void Corner_Detection(void)
{
	 u8 i;
	 int16_t temp;
	Weidu=Ascii_to_long(GPS.Buf_WeiDu);		 			//	转换成整数型
	Jingdu=Ascii_to_long(GPS.Buf_JingDu);	 				//	转换成整数型	
	Course=Ascii_to_int(GPS.Buf_Course);
	Left_Stand_Speech();

	for(i=0;i<35;i++)
	{
	   if(((Weidu-dot[i][0])<=100)&&((Weidu-dot[i][0])>=-100)&&((Jingdu-dot[i][1])<=100)&&((Jingdu-dot[i][1])>=-100))//判断第一个进弯点
	   {	
			  Alarm_Location_W=i+101;
			  Site=1;				  //弯道语音提示标志位
			  
	  		  if(i==0x00&&First_Dot==1)
			  {
				  break; 
			  }	
			   
			  Flag_Corner=1; 		//检测弯道的标志位
			  if((Course-Dot_Course_Angle[i][0])>0)
			  {
			  		temp=Course-Dot_Course_Angle[i][0];
				  	if(((temp<90)&&(temp>0))||((temp<360)&&(temp>270)))
					{
					  Flag_Into=0;
					  if(i==0x01)	  //消除第一个弯道乱报
					  {
					  	First_Dot=1;	
					  }
					}
					else
					{
						Flag_Into=1;	
					}
			  }
			  else 
			  {
					temp=Dot_Course_Angle[i][0]-Course;
					if(((temp<90)&&(temp>0))||((temp<360)&&(temp>270)))
					{
					  Flag_Into=0;
					  if(i==0x01)		  //消除第一个弯道乱报
					  {
					  	First_Dot=1;	
					  }
					}
					else
					{
						Flag_Into=1;	
					}
			  }			 
			  break;
			  	
	   }
	   else if(((Weidu-dot[i][2])<=100)&&((Weidu-dot[i][2])>=-100)&&((Jingdu-dot[i][3])<=100)&&((Jingdu-dot[i][3])>=-100))//判断第二个进弯点
	   {
			  Alarm_Location_W=i+101;
			  Site=1;				   //弯道语音提示标志位	  

			  if(i==0x00&&First_Dot==1)
			  {
				  break; 
			  }				   
//			  if(i==0x00)
//			  {
//				   if(!(((Weidu-dot[i][2])<=50)&&((Weidu-dot[i][2])>=-50)&&((Jingdu-dot[i][3])<=50)&&((Jingdu-dot[i][3])>=-50)))
//				   break;
//			  }	  

			  Flag_Corner=1;			//检测弯道的标志位
			  if((Course-Dot_Course_Angle[i][1])>0)
			  {
			  		temp=Course-Dot_Course_Angle[i][1];
				  	if(((temp<90)&&(temp>0))||((temp<360)&&(temp>270)))
					{
					  Flag_Into=0;
					}
					else
					{
						Flag_Into=1;	
					}
			  }
			  else 
			  {
					temp=Dot_Course_Angle[i][1]-Course;
					if(((temp<90)&&(temp>0))||((temp<360)&&(temp>270)))
					{
					  Flag_Into=0;
					}
					else
					{
						Flag_Into=1;	
					}
			  }
			  break;
			
	   }	   			   		   		   
	}
	if(!Flag_Corner)
	for(i=0;i<51;i++)
	{								 //判断进站点
	   
	   	if(i==3||i==4)
		{
			 if(((Weidu-Stand[i][0])<=180)&&((Weidu-Stand[i][0])>=-180)&&((Jingdu-Stand[i][1])<=180)&&((Jingdu-Stand[i][1])>=-180))
			 {
				   Alarm_Location_Z=i+1;
				   Straight_Speeding_Value=shili_to_shuirao;
			 }
		}
		if(i>45)   //景区外
		{
			 if(((Weidu-Stand[i][0])<=200)&&((Weidu-Stand[i][0])>=-200)&&((Jingdu-Stand[i][1])<=200)&&((Jingdu-Stand[i][1])>=-200))
			 {
				   Alarm_Location_Z=0;
				   Alarm_Location_W=0;
				   Straight_Speeding_Value=99;
				   Flag_Corner=1;
				   Flag_Into=1;
			 }
		}
	    else if(((Weidu-Stand[i][0])<=80)&&((Weidu-Stand[i][0])>=-80)&&((Jingdu-Stand[i][1])<=80)&&((Jingdu-Stand[i][1])>=-80))	 
	    {	
			  Alarm_Location_Z=i+1;
			  Site=i+2;				  //进站语音标志位
			  switch(i)
			  {
				  case 0x03:Straight_Speeding_Value=shili_to_shuirao;
						     break; 
				  case 0x04:Straight_Speeding_Value=shili_to_shuirao;
						     break;
				  case 0x21:Straight_Speeding_Value=laomo_to_huangshi; //33
						     break; 
				  case 0x22:Straight_Speeding_Value=laomo_to_huangshi;	//34
						     break; 
				  case 0x15:Straight_Speeding_Value=sancha_to_tongqiang; //21 三岔路
						     break;
				  case 0x2a:Straight_Speeding_Value=sancha_to_tongqiang; //42 铜墙铁壁
						     break; 
				  default :Straight_Speeding_Value=Other_Speeding_Value;
						     break;
			  } 
			  Flag_Corner=1; 		//检测弯道的标志位
			  if((Course-Stand_Course_Angle[i][0])>0)
			  {
			  		temp=Course-Stand_Course_Angle[i][0];
				  	if(((temp<90)&&(temp>0))||((temp<360)&&(temp>270)))
					{
					  Flag_Into=0;
					}
					else
					{
					   Flag_Into=1;
					}
			  }
			  else 
			  {
					temp=Stand_Course_Angle[i][0]-Course;
					if(((temp<90)&&(temp>0))||((temp<360)&&(temp>270)))
					{
					  Flag_Into=0;
					}
					else  
					{
					   Flag_Into=1;
					}
				
			  }
			  break;	
			
	   }											  //判断离站点
	   else if(((Weidu-Stand[i][2])<=80)&&((Weidu-Stand[i][2])>=-80)&&((Jingdu-Stand[i][3])<=80)&&((Jingdu-Stand[i][3])>=-80))
	   {
			  Alarm_Location_Z=i+1;
			  Site=0;				   //离站 温馨提示标志位
			  switch(i)				   //超速值更改
			  {
				  case 0x03:Straight_Speeding_Value=shili_to_shuirao;
						     break; 
				  case 0x04:Straight_Speeding_Value=shili_to_shuirao;
						     break;
				  case 0x21:Straight_Speeding_Value=laomo_to_huangshi;
						     break; 
				  case 0x22:Straight_Speeding_Value=laomo_to_huangshi;
						     break; 
				  case 0x15:Straight_Speeding_Value=sancha_to_tongqiang;   //21 三岔路
						     break;
				  case 0x2a:Straight_Speeding_Value=sancha_to_tongqiang;    //42 铜墙铁壁
						     break; 
				  default:Straight_Speeding_Value=Other_Speeding_Value;
						     break;
			  }   
			  Flag_Corner=1;			//检测弯道的标志位
			  if((Course-Stand_Course_Angle[i][1])>0)
			  {
			  		temp=Course-Stand_Course_Angle[i][1];
				  	if(((temp<90)&&(temp>0))||((temp<360)&&(temp>270)))
					{
					  Flag_Into=0;
					}
					else
					{
					   Flag_Into=1;
					}
			  }
			  else 
			  {
					temp=Stand_Course_Angle[i][1]-Course;
					if(((temp<90)&&(temp>0))||((temp<360)&&(temp>270)))
					{
					  Flag_Into=0;
					}
					else  
					{
					   Flag_Into=1;
					}
				
			  }

			  if((0==i||8==i||23==i||33==i||35==i)&&1==Flag_Into)  //出站口方向
			  {
				   Alarm_Location_Z=0;
				   Alarm_Location_W=0;
				   Straight_Speeding_Value=99;
			  }

			  break;
				
	     }
	}
	if((Flag_Corner==1))	   
	{
		Flag_Corner=0;
		GPIO_ResetBits(GPIOA,GPIO_Pin_6);
		if(Flag_Into==0) //进弯
		{							   			
				   		//进弯指示灯亮
 			if(Corner_Speech&&Lock_TIM2)	//第一次进弯 且速度大于3
			{
				Corner_Speech=0;
				if(Alarm_Location_W==101)
				{
				   First_Dot=1;
				}
				else if(Alarm_Location_W>102||Site>1)
				{
				   First_Dot=0;
				}				
				Speech_Play(Site);
				if(Site==1)
				{								
					Time_Over_W=7;		  //3s 7---4---2
					Lock_TIM3_W=1;
					TIM2->CR1|=0x0001;    //使能定时器2		   
				}
		
			}	  			
			if(Site>1)
			{
			  	Stop=0;
			}			
		}		
	}
	else
	{	 	  
		GPIO_SetBits(GPIOA,GPIO_Pin_6);
		Corner_Speech=1;		  	   	    
	}
	  	  
}


/************************************
函数功能：ASCII码字符串转长整数
函数入口：*buf 待转换的ASCII码
函数出口：无
函数说明：
***********************************/
long int  Ascii_to_long(u8 *buf)
{
	long int rev=0;
	int  s=0;
	u8  ch;

	while((*buf) != '\0')
	{
		ch=*buf;
		 *buf++;
		switch(ch)
		{
			case '0':  s=0;break;
			case '1':  s=1;break;
			case '2':  s=2;break;
			case '3':  s=3;break;
			case '4':  s=4;break;
			case '5':  s=5;break;
			case '6':  s=6;break;
			case '7':  s=7;break;
			case '8':  s=8;break;
			case '9':  s=9;break;
			case '.':  s='.';break;
			default : break;
		}

		if(s=='.')
		{
			continue;
		}
		else
		{
		  rev=rev*10+s;
		}
		
	}
	return rev;
}

/************************************
函数功能：ASCII码字符串转实数
函数入口：*buf 待转换的ASCII码
函数出口：无
函数说明：
***********************************/
float  Ascii_to_float(u8 *buf)
{
	float rev=0,s=0;
	int  i;
	u8  ch,Dec=0;

	while((*buf) != '\0')
	{
		ch=*buf;
		*buf++;
		switch(ch)
		{
			case '0':  s=0;break;
			case '1':  s=1;break;
			case '2':  s=2;break;
			case '3':  s=3;break;
			case '4':  s=4;break;
			case '5':  s=5;break;
			case '6':  s=6;break;
			case '7':  s=7;break;
			case '8':  s=8;break;
			case '9':  s=9;break;
			case '.':  s='.';break;
			default : break;
		}

		if(s=='.')
		{
			i=1;
			Dec=1;
			continue;
		}
		else
		{
			if(Dec==1)
			{
				rev=rev+s/(i*10);
				i=i*10;
			}
			else
			{
		  		rev=rev*10+s;
			}
		}
		
	}
	return rev;
}

void GPS_TO_GPRS_Data(void)
{
	u8 i=0,j=0;
	u16 S;
	while(GPS.Buf_WeiDu[i]!='\0')
	{
	
		if(GPS.Buf_WeiDu[i]!='.')
		{
			weidu[j++]=GPS.Buf_WeiDu[i];	
		}
		i++	 ;
	}
   	i=0;
	j=0;
    while(GPS.Buf_JingDu[i]!='\0')
	{
	
		if(GPS.Buf_JingDu[i]!='.')
		{
			jingdu[j++]=GPS.Buf_JingDu[i];	
		}
		i++	 ;
	}
	
	for(i=0;i<6;i++)
	{
	  time[i]=GPS.Buf_Time[i];
	}
	time_chang[0]=time[0];
	time_chang[1]=time[1];
	time_int=Ascii_to_int(time_chang)+8;
	time[0]=time_int/10+0x30;
	time[1]=time_int%10+0x30;



	for(i=0;i<6;i++)
	{
	  date[i]=GPS.Buf_Data[i];
	}
	i=0;
	j=0;
   
	while(GPS.Buf_Speed[i]!='\0')
	{
		
		if(GPS.Buf_Speed[i]=='.')
		{
			switch(i)
			{
				case 0x01:{
							speed2[3]=GPS.Buf_Speed[2];
							speed2[2]=GPS.Buf_Speed[0];
							speed2[1]='0';
							speed2[0]='0';
						  };break;
				case 0x02:{
							speed2[3]=GPS.Buf_Speed[3];
							speed2[2]=GPS.Buf_Speed[1];
							speed2[1]=GPS.Buf_Speed[0];
							speed2[0]='0';
						  };break;
				case 0x03:{
							speed2[3]=GPS.Buf_Speed[4];
							speed2[2]=GPS.Buf_Speed[2];
							speed2[1]=GPS.Buf_Speed[1];
							speed2[0]=GPS.Buf_Speed[0];
						  };break;
			}	
		}
		i++;	
	}
		   
   S=Ascii_to_int(GPS.Buf_Speed)/2;
   
   speed1[0]=S/16;

   speed1[1]=S%16;  
   
   if(speed1[0]>=10)
       speed1[0]+=0x37;
   else
   	   speed1[0]+=0x30;
   
   if(speed1[1]>=10)
       speed1[1]+=0x37;
   else
   	   speed1[1]+=0x30;
   
//   GPRS_Send_flag=1;

}


void GPS_TO_GPRS_Alarm_Data(void)
{
	u8 i=0,j=0;
	u16 S=0;
	float  Test=0;
	while(GPS.Buf_WeiDu[i]!='\0')
	{
	
		if(GPS.Buf_WeiDu[i]!='.')
		{
			weidu_Alarm[j++]=GPS.Buf_WeiDu[i];	
		}
		i++	 ;
	}
   	i=0;
	j=0;
    while(GPS.Buf_JingDu[i]!='\0')
	{
	
		if(GPS.Buf_JingDu[i]!='.')
		{
			jingdu_Alarm[j++]=GPS.Buf_JingDu[i];	
		}
		i++	 ;
	}
	
	for(i=0;i<6;i++)
	{
	  time_Alarm[i]=GPS.Buf_Time[i];
	}
	time_chang[0]=time_Alarm[0];
	time_chang[1]=time_Alarm[1];
	time_int=Ascii_to_int(time_chang)+8;
	time_Alarm[0]=time_int/10+0x30;
	time_Alarm[1]=time_int%10+0x30;

//	UART2_SendString("Buffer_Send:");
//	UART2_SendString(time_Alarm);
//	UART2_SendString("--");

	for(i=0;i<6;i++)
	{
	  date_Alarm[i]=GPS.Buf_Data[i];
	}
			   
   S=Ascii_to_int(GPS.Buf_Speed)/2+1;
   Test=(S-1)*2*1.852;
   if((Alarm&&Test>(Straight_Speeding_Value*0.2+Straight_Speeding_Value)&&Test<120)||(Alarm_W_Flag&&(Test>(Corner_Speeding_Value*0.2+Corner_Speeding_Value))&&Test<60))
   {   
	   speed1_Alarm[0]=S/16;	
	   speed1_Alarm[1]=S%16;  
	   
	   if(speed1_Alarm[0]>=10)
	       speed1_Alarm[0]+=0x37;
	   else
	   	   speed1_Alarm[0]+=0x30;
	   
	   if(speed1_Alarm[1]>=10)
	       speed1_Alarm[1]+=0x37;
	   else
	   	   speed1_Alarm[1]+=0x30;	


		i=0;
		j=0;
		while(GPS.Buf_Speed[i]!='\0')
		{
			
			if(GPS.Buf_Speed[i]=='.')
			{
				switch(i)
				{
					case 0x01:{
								speed2_Alarm[3]=GPS.Buf_Speed[2];
								speed2_Alarm[2]=GPS.Buf_Speed[0];
								speed2_Alarm[1]='0';
								speed2_Alarm[0]='0';
								if(speed2_Alarm[2]>0x37&&speed2_Alarm[2]<0x3A)
								{
									speed2_Alarm[2]=speed2_Alarm[2]-0x08;
									speed2_Alarm[1]=speed2_Alarm[1]+0x01;	
								}
								else
								{
								   speed2_Alarm[2]=speed2_Alarm[2]+0x02;
								}
							  };break;
					case 0x02:{
								speed2_Alarm[3]=GPS.Buf_Speed[3];
								speed2_Alarm[2]=GPS.Buf_Speed[1];
								speed2_Alarm[1]=GPS.Buf_Speed[0];
								speed2_Alarm[0]='0';
								if(speed2_Alarm[2]>0x37&&speed2_Alarm[2]<0x3A)
								{
									speed2_Alarm[2]=speed2_Alarm[2]-0x08;
									speed2_Alarm[1]=speed2_Alarm[1]+0x01;
									if(speed2_Alarm[1]==0x3A)
									{
									   speed2_Alarm[1]=speed2_Alarm[1]-10;
									   speed2_Alarm[0]=speed2_Alarm[0]+0x01;
									}	
								}
								else
								{
								   speed2_Alarm[2]=speed2_Alarm[2]+0x02;
								}
							  };break;
					case 0x03:{
								speed2_Alarm[3]=GPS.Buf_Speed[4];
								speed2_Alarm[2]=GPS.Buf_Speed[2];
								speed2_Alarm[1]=GPS.Buf_Speed[1];
								speed2_Alarm[0]=GPS.Buf_Speed[0];
								if(speed2_Alarm[2]>0x37&&speed2_Alarm[2]<0x3A)
								{
									speed2_Alarm[2]=speed2_Alarm[2]-0x08;
									speed2_Alarm[1]=speed2_Alarm[1]+0x01;	
									if(speed2_Alarm[1]==0x3A)
									{
									   speed2_Alarm[1]=speed2_Alarm[1]-10;
									   speed2_Alarm[0]=speed2_Alarm[0]+0x01;
									}
								}
								else
								{
								   speed2_Alarm[2]=speed2_Alarm[2]+0x02;
								}
							  };break;
				}	
			}
			i++;	
		}

   }
   else
   {
		Alarm=0;
		Alarm_W=0;
		GPS_TO_GPRS_Data();
		Function=3;
   }
}

/************************************
函数功能：ASCII码字符串转长整数
函数入口：*buf 待转换的ASCII码
函数出口：无
函数说明：
***********************************/
int16_t  Ascii_to_int(u8 *buf)
{
	int16_t rev=0;
	int16_t  s=0;
	u8  ch;

	while((*buf) != '\0')
	{
		ch=*buf;
		*buf++;
		switch(ch)
		{
			case '0':  s=0;break;
			case '1':  s=1;break;
			case '2':  s=2;break;
			case '3':  s=3;break;
			case '4':  s=4;break;
			case '5':  s=5;break;
			case '6':  s=6;break;
			case '7':  s=7;break;
			case '8':  s=8;break;
			case '9':  s=9;break;
			case '.':  s='.';break;
			default : break;
		}

		if(s=='.')
		{
			return rev;
		//	break;

		}
		else
		{
		  rev=rev*10+s;
		}
		
	}
	return rev;
}

